package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.ConanMenuService;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.itheima.constant.MessageConstant.GET_MENU_FAIL;
import static com.itheima.constant.MessageConstant.GET_MENU_SUCCESS;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: ConanController
 * @Author: WuKenan
 * @Date: 2019/12/14 11:02
 * @Description: //TODO
 */
@RestController
@RequestMapping("/conanmenu")
public class ConanController {

        @Reference
        private ConanMenuService conanService;


        /**
         * 根据用户权限加载页面菜单项
         */
        @RequestMapping("/getMenus")
        private Result getMenus() {
            //获取该登录的用户
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            //根据用户的角色获取应有的权限列表
            List<Menu> menu = conanService.getMenus(user.getUsername());

            if (menu.size() > 0) {
                return new Result(true,GET_MENU_SUCCESS, menu);
            } else {
                return new Result(false,GET_MENU_FAIL);
            }
        }


}
